package com.example.controller;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.example.annotation.Log;
import com.example.common.result.Result;
import com.example.entity.dto.ZnxDuiFaDto;
import com.example.entity.hz.Znx;
import com.example.mapper.ZnxMapper;
import com.example.service.ZnxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.example.config.WebSocketMessage.sendMessage;

//@CrossOrigin // 跨域
@RestController
@RequestMapping("/api/Znx")
@Api(tags = "站内信控制台")
public class ZnxController {

    @Autowired
    private ZnxService znxService;
    @Autowired
    private ZnxMapper znxMapper;

    @PostMapping("/insertZnx")
    @ApiOperation("新增站内信")
    @Log("新增站内信")
    public Result insert(@RequestBody List<Znx> znx){
       return this.znxService.insert(znx);
    }

    @PostMapping("/insertZnxS")
    @ApiOperation("新增站内信S java类中使用")
    @Log("新增站内信S java类中使用")
    public Result insertZnxS(List<Znx> znx){
        return this.znxService.insert(znx);
    }

    @ApiOperation("站内信查询")
    @GetMapping("/queryList/{current}/{size}/{notificationCode}")
    public  Result<Map<String,Znx>> queryList(@PathVariable Integer current, @PathVariable Integer size, @PathVariable String notificationCode){
        return this.znxService.queryList(current,size,notificationCode);
    }

    @ApiOperation("修改状态")
    @GetMapping("/update/{id}/{status}")
    public Result update(@PathVariable Integer id,@PathVariable Integer status){
        return this.znxService.update(id,status);
    }

    @ApiOperation("給我發A，給你發A")
    @PostMapping("/duifa")
    public Result duifa(@RequestBody ZnxDuiFaDto dto){

        try {
            sendMessage(dto.getUser(), dto.getMessage());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return Result.success();
    }

    @ApiOperation("一键已读,修改状态 后端直接修改状态")
    @PostMapping("/updateBath")
    @DSTransactional
    public Result updateBath(@RequestBody List<Integer> znxList){
        Wrapper<Znx> updateWrapper = new UpdateWrapper<Znx>()
                .set("status", 1)
                .in("id", znxList);// 这里使用 in 条件进行更新
        this.znxService.update(updateWrapper);

//        this.znxService.updateStatusByList(znxList);

        return Result.success();
    }


}
